package com.dalor.phonesmanager.activity;

import android.Manifest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.dalor.phonesmanager.bean.Folder;
import com.dalor.phonesmanager.adapter.GridPhoneAdapter;
import com.dalor.phonesmanager.interfaces.OnImageLoaderListener;
import com.dalor.phonesmanager.utils.PermissionUtil;
import com.dalor.phonesmanager.utils.QueryImageUtil;
import com.dalor.phonesmanager.R;

import java.util.ArrayList;

/**
 * 展示图片的目录列表
 */
public class MainActivity extends AppCompatActivity {

    private String[] permissions=new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private static final int LOADER_ALL = 0;
    private GridView gridView;
    private GridPhoneAdapter adapter;
    private ArrayList<Folder> folders=new ArrayList<Folder>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PermissionUtil.getPermission(permissions,this);
        gridView=(GridView)findViewById(R.id.gv_phone);
        adapter=new GridPhoneAdapter(this);
        gridView.setAdapter(adapter);
        getSupportLoaderManager().restartLoader(LOADER_ALL,
                null,
                QueryImageUtil.getInstance(getApplicationContext(),loaderListener).mLoaderCallback);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                showPhones(i);
            }
        });
    }

    private void showPhones(int i){
        Intent intent=new Intent(this,PhonesActivity.class);
        Bundle bundle=new Bundle();
        bundle.putParcelableArrayList("phones",folders.get(i).images);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    private OnImageLoaderListener loaderListener=new OnImageLoaderListener() {
        @Override
        public void onImages(ArrayList<String> resultList) {

        }

        @Override
        public void onImageFolders(ArrayList<Folder> mResultFolder) {
                folders.addAll(mResultFolder);
                adapter.setData(mResultFolder);
        }
    };

}
